'use strict'

class saveTecho {

    constructor() {
        this.param = '/'
    }

    async post(c) {
        try {
            var openid = c.box.user.openid;
            var str = c.body.toString('utf-8');
            var techoObj = JSON.parse(str).text;
            var length = techoObj.length;

            // 找出模板--bgimage
            var template_id;
            var isNullId = "";
            for (var i = 0; i < length; i++) {
                if (techoObj[i].type == 'bgImage') {
                    template_id = techoObj[i].template_cover;
                    if (techoObj[i].template_id == null) {
                        isNullId = i;
                    }
                    break;
                }
                if (techoObj[i].type == 'bgColor') {
                    template_id = techoObj[i].color
                    break
                }
                else { template_id = 0; }//无模板
            }

            // techo表处理 
            if (isNullId === "") {
                var result = await c.service.psql.query(`select MAX(techo_id) from techo`);
                var techo_id = result.rows[0].max;
                if (techo_id == null) {
                    techo_id = 1;
                } else {
                    techo_id++;
                }
                var sqltext0 = `insert into techo (techo_id,openid,techo_template_id) VALUES` +
                    ` (${techo_id},'${openid}','${template_id}');`
                var r = await c.service.psql.query(sqltext0);
            } else {//null
                var techo_id = techoObj[isNullId].techo_id;
                // 删除
                var delSql = `delete from techo_sticker where techo_id=${techo_id};`
                +`delete from techo_picture where techo_id=${techo_id};`
                +`delete from techo_word where techo_id=${techo_id};`
                +`delete from techo_shade where techo_id=${techo_id};`
                await c.service.psql.query(delSql);
            }

            //其余处理
            for (var i = 0; i < length; i++) {
                // 贴纸
                if (techoObj[i].type == 'sticker') {
                    var rSti = await c.service.psql.query(`select Max(id) from techo_sticker`);
                    var id = rSti.rows[0].max;
                    if (id == null) {
                        id = 1
                    } else {
                        id++;
                    }
                    var address = '/home/ubuntu/sucaiku/stickers/' + techoObj[i].sticker_name;
                    var sqltext1 = `insert into techo_sticker (id,sticker_id,techo_id,`
                        + `openid,sticker_address,sticker_rotate,y,x,w,h,num,sticker_name) `
                        + `values(${id},${techoObj[i].sticker_id},${techo_id},'${openid}','${address}',`
                        + `${Math.round(techoObj[i].rotate)},${Math.round(techoObj[i].y)},`
                        + `${Math.round(techoObj[i].x)},${Math.round(techoObj[i].w)},${Math.round(techoObj[i].h)},${i},'${techoObj[i].sticker_name}');`
                    await c.service.psql.query(sqltext1);
                }

                if (techoObj[i].type == 'shape') {
                    console.log(techoObj[i]);
                    var r6 = await c.service.psql.query('select Max(shade_id) from techo_shade');
                    var shade_id = r6.rows[0].max;
                    if (shade_id == null) {
                        shade_id = 1;
                    } else {
                        shade_id++;
                    }
                    var sqltextS = `insert into techo_shade (shade_id,techo_id,openid,shade_address,y,x,w,h,num) `
                        + `values(${shade_id},${techo_id},'${openid}','${techoObj[i].url}',`
                        + `${Math.round(techoObj[i].y)},${Math.round(techoObj[i].x)},${Math.round(techoObj[i].w)},${Math.round(techoObj[i].h)},${i})`
                    await c.service.psql.query(sqltextS);
                }


                if (techoObj[i].type == 'text') {
                    // 文字
                    var result = await c.service.psql.query(`select MAX(word_id) from techo_word`);
                    var word_id = result.rows[0].max;
                    if (word_id == null) {
                        word_id = 1;
                    } else {
                        word_id++;
                    }
                    var sqltext3 = `INSERT INTO techo_word(word_id,techo_id,openid,`
                        + `word_content,word_color,word_rotate,y,x,w,h,fontsize,num) `
                        + `values(${word_id},${techo_id},'${openid}','${techoObj[i].text}','${techoObj[i].color}',${Math.round(techoObj[i].rotate)},${Math.round(techoObj[i].y)},${Math.round(techoObj[i].x)},${Math.round(techoObj[i].w)},${Math.round(techoObj[i].h)},${techoObj[i].fontSize},${i});`
                    await c.service.psql.query(sqltext3);
                }
            }
            return c.send('保存成功了');
        } catch (err) {
            console.log(err);
            return
        }
    }
}

module.exports = saveTecho